package main

import (
	"code.oldboy.com/studygo/LogAgent/conf"
	"code.oldboy.com/studygo/LogAgent/etcd"
	"code.oldboy.com/studygo/LogAgent/kafka"
	"code.oldboy.com/studygo/LogAgent/tailLogManager"
	"code.oldboy.com/studygo/LogAgent/util"
	"fmt"
	"gopkg.in/ini.v1"
	"sync"
	"time"
)
var cfg =new(conf.AppConfig)



func main() {
	//配置文件
	err := ini.MapTo(cfg, "./conf/config.ini")
	if err!=nil {
		fmt.Println(err)
	}
	//初始化kafka
	err = kafka.Init([]string{cfg.KafkaConfig.Address},cfg.Timeout)
	if err!=nil {
		fmt.Println("init kafka error:=",err)
		return
	}
	//初始化etcd
	err = etcd.Init(cfg.EtcdConfig.Address,time.Duration(cfg.EtcdConfig.Timeout))
	if err!=nil {
		fmt.Println("etcd init error:=",err)
	}
	ip, err := util.GetIp()
	if err!=nil {
		panic(err)
	}
	key:=fmt.Sprintf(cfg.EtcdConfig.EtcdKey,ip)
	fmt.Println("key:=",key)
	read, err := etcd.Read(key)
	time.Sleep(time.Second*5)
	if err!=nil {
		fmt.Println("err read ",err)
	}
	tailLogManager.Init(read)
	var wg sync.WaitGroup
	go etcd.WatchConf(key,tailLogManager.GetNewConfChan())
	wg.Add(1)
	wg.Wait()

}
